package com.sky.controller.admin;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zzw
 * @date 2025/4/25
 * @description 菜品分类控制器
 */
@Api(tags = "分类管理接口")
@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 修改分类
     *
     * @param category 分类
     * @return Result<Category>
     */
    @ApiOperation("修改分类")
    @PutMapping
    public Result<Category> update(@RequestBody @ApiParam("分类实体") Category category) {
        log.info("修改分类:{}", category);
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 分页查询分类
     *
     * @param categoryPageQueryDTO 分类分页查询传输对象
     * @return Result<PageResult < Category>>
     */
    @ApiOperation("分页查询分类")
    @GetMapping("/page")
    public Result<PageResult<Category>> page(@ApiParam("分页查询条件") CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询:{}", categoryPageQueryDTO);
        PageResult<Category> pageResult = categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用、禁用分类
     *
     * @param status 状态 1为启用，0为禁用
     * @return Result<String>
     */
    @ApiOperation("启用禁用分类")
    @PostMapping("/status/{status}")
    public Result<String> status(@PathVariable @ApiParam("状态值:1启用/0禁用") Integer status,
                                 @ApiParam("分类ID") Long id) {
        log.info("启用禁用分类:{}", status);
        categoryService.status(status, id);
        return Result.success();
    }

    /**
     * 新增分类
     *
     * @param category 分类
     * @return Result<String>
     */
    @ApiOperation("新增分类")
    @PostMapping
    public Result<String> save(@RequestBody @ApiParam("分类实体") Category category) {
        log.info("新增分类:{}", category);
        categoryService.save(category);
        return Result.success();
    }

    /**
     * 根据id删除分类
     *
     * @param id 分类id
     * @return Result<String>
     */
    @ApiOperation("根据ID删除分类")
    @DeleteMapping
    public Result<String> deleteById(@ApiParam("分类ID") Long id) {
        log.info("根据id删除分类:{}", id);
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     *
     * @param type 类型
     * @return Result<List < Category>>
     */
    @ApiOperation("根据类型查询分类列表")
    @GetMapping("/list")
    public Result<List<Category>> list(@ApiParam("分类类型") Integer type) {
        log.info("根据类型查询分类:{}", type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }
}